<?php
namespace App\Services\Wxxcx;

class UserService extends BaseService
{
    /**
     * 微信小程序
     */

    /**
     * 授权
     * @param array $pas
     */
    public function auth($pas)
    {
        $uOne = (array) $this->dbConn($this->dbUser)
            ->where('openid', $pas['openid'])
            ->first();
        if (empty($uOne)) {
            $wxData = [
                'nickName' => $pas['nickName'],
                'avatarUrl' => $pas['avatarUrl'],
                'openid' => $pas['openid'],
                'wx_json' => json_encode($pas['userInfo'], JSON_UNESCAPED_UNICODE),
                'addtime' => toDate(),
                'uptime' => toDate(),
            ];
            $uid = $this->dbConn($this->dbUser)->insertGetId($wxData);
            if (!$uid) {
                return $this->toback(-2, 'error');
            }
        } else {
            $wxData = [
                'nickName' => $pas['nickName'],
                'avatarUrl' => $pas['avatarUrl'],
                'openid' => $pas['openid'],
                'wx_json' => json_encode($pas['userInfo'], JSON_UNESCAPED_UNICODE),
                'uptime' => toDate(),
            ];
            $up = $this->dbConn($this->dbUser)->where('id', $uOne['id'])->update($wxData);
            if (!$up) {
                return $this->toback(-2, 'error');
            }
            $uid = $uOne['id'];
        }
        $h5codeOne = (array) $this->dbConn($this->dbH5code)
            ->where('h5code', $pas['h5code'])
            ->first();
        if (!empty($h5codeOne)) {
            $this->dbConn($this->dbH5code)->where('h5code', $pas['h5code'])
                ->update(['uid' => $uid, 'utime' => toDate()]);
        }
        $resData = [
            'uid' => $uid,
            'nickName' => $pas['nickName'],
            'avatarUrl' => $pas['avatarUrl'],
        ];
        return $this->toback(0, '', $resData);
    }

    /**
     * 获取统计数据
     * @param int $uid
     */
    public function tongji($uid)
    {
        $arys = [
            'uname' => '',
            'uhead' => '',
            'udesc' => '',
            'scan_total' => 0,
            'follow_total' => 0,
            'reply_total' => 0,
            'scene_total' => 0,
            'fuwu_total' => 0,
            'ad_total' => 0,
        ];
        $uOne = (array) $this->dbConn($this->dbUser)
            ->where('id', $uid)
            ->first();
        if (empty($uOne)) {
            return $this->toback(-2, '没有记录');
        }
        $h5Ones = $this->dbConn($this->dbH5code)
            ->where('uid', $uid)
            ->get();
        $h5codes = [];
        if (count($h5Ones)) {
            foreach ($h5Ones as $v) {
                $h5codes[] = $v->h5code;
            }
        }
        $arys['uname'] = $uOne['nickName'];
        $arys['uhead'] = $uOne['avatarUrl'];
        $day_num = ceil((time() - strtotime($uOne['addtime'])) / 3600 /24);
        $arys['udesc'] = '已使用' . $day_num . '天';
        //扫码数
        $arys['scan_total'] = $this->dbConn($this->dbQrlog)
            ->where('uid', $uid)
            ->count();
        //喜欢数
        $arys['follow_total'] = $this->dbConn($this->dbTmpFollow)
            //->where('uid', $uid)
            ->whereIn('h5code', $h5codes)
            ->count();
        //回复数
        $arys['reply_total'] = $this->dbConn($this->dbTmpReply)
            //->where('uid', $uid)
            ->whereIn('h5code', $h5codes)
            ->count();
        //场景数
        $arys['scene_total'] = $this->dbConn($this->dbScene)
            ->where('del', 1)
            ->count();
        //服务数
        $arys['fuwu_total'] = $this->dbConn($this->dbFuwu)
            ->where('del', 1)
            ->where('type', '>', 0)
            ->count();
        //广告数
        $arys['ad_total'] = $this->dbConn($this->dbAdList)
            ->where('del', 1)
            ->count();
        return $this->toback(0, '', $arys);
    }

    /**
     * 扫码列表
     * @param int $uid
     * @param array $pages【start，limit】
     */
    public function getScanList($uid, $pages)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = $this->dbConn($this->dbQrlog)->where('uid', $uid);
        $total = $db->count();
        $lists = $db->skip($start)->take($pages['limit'])
            ->orderBy('id', 'desc')
            ->get();
        if (!count($lists)) {
            return $this->toback(-2, '没有记录');
        }
        $arys = [];
        foreach ($lists as $v) {
            $v = (array) $v;
            $tmpOne = (array) $this->dbConn($this->dbTmp)
                ->where('id', $v['tmp_id'])
                ->first();
            $title = '';
            $thumb = '';
            $scene_id = 0;
            if (!empty($tmpOne)) {
                $title = $tmpOne['title'];
                $scene_id = $tmpOne['scene_id'];
                if (file_exists($tmpOne['thumb'])) {
                    $thumb = $tmpOne['thumb'];
                }
            }
            $sceneOne = (array) $this->dbConn($this->dbScene)
                ->where('id', $scene_id)
                ->first();
            $scene_name = !empty($sceneOne) ? $sceneOne['title'] : '-';
            $visit_total = $this->dbConn($this->dbQrlog)
                ->where('tmp_id', $v['tmp_id'])
                ->count();
            $arys[] = [
                'id' => $v['id'],
                'title' => $title,
                'scene_name' => $scene_name,
                'code' => $v['code'],
                'prefixUrl' => env('DOMAIN').'/',
                'thumb' => $thumb,
                'visit_total' => $visit_total,
                'addtime' => $v['addtime'],
            ];
        }
        return $this->toback(0, '', $arys, $total);
    }

    /**
     * 关注列表
     * @param int $uid
     * @param array $pages【start，limit】
     */
    public function getFollowList($uid, $pages)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = $this->dbConn($this->dbTmpFollow)
            ->where('uid', $uid)
            ->where('del', 1);
        $total = $db->count();
        $lists = $db->skip($start)->take($pages['limit'])->get();
        if (!count($lists)) {
            return $this->toback(0, '没有记录');
        }
        $arys = [];
        foreach ($lists as $v) {
            $v = (array) $v;
            $tmpOne = (array) $this->dbConn($this->dbTmp)
                ->where('id', $v['tmp_id'])
                ->first();
            $title = '';
            $thumb = '';
            $code = '';
            $scene_id = 0;
            if (!empty($tmpOne)) {
                $title = $tmpOne['title'];
                $scene_id = $tmpOne['scene_id'];
                $code = $tmpOne['code'];
                if (file_exists($tmpOne['thumb'])) {
                    $thumb = $tmpOne['thumb'];
                }
            }
            $sceneOne = (array) $this->dbConn($this->dbScene)
                ->where('id', $scene_id)
                ->first();
            $scene_name = empty($sceneOne) ? $sceneOne['title'] : '-';
            $follow_total = $this->dbConn($this->dbTmpFollow)
                ->where('tmp_id', $v['tmp_id'])
                ->count();
            $arys[] = [
                'id' => $v['id'],
                'title' => $title,
                'scene_name' => $scene_name,
                'code' => $code,
                'prefixUrl' => env('DOMAIN').'/',
                'thumb' => $thumb,
                'follow_total' => $follow_total,
                'addtime' => $v['addtime'],
            ];
        }
        return $this->toback(0, '', $arys, $total);
    }

    /**
     * 评论列表
     * @param int $uid
     * @param array $pages【start，limit】
     */
    public function getReplyList($uid, $pages)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = $this->dbConn($this->dbTmpReply)
            ->where('uid', $uid)
            ->where('del', 1);
        $total = $db->count();
        $lists = $db->skip($start)->take($pages['limit'])
            ->orderBy('id', 'desc')
            ->get();
        if (!count($lists)) {
            return $this->toback(0, '没有记录');
        }
        $arys = [];
        foreach ($lists as $v) {
            $v = (array) $v;
            $tmpOne = (array) $this->dbConn($this->dbTmp)
                ->where('id', $v['tmp_id'])
                ->first();
            $title = '';
            $thumb = '';
            $code = '';
            $scene_id = 0;
            if (!empty($tmpOne)) {
                $title = $tmpOne['title'];
                $scene_id = $tmpOne['scene_id'];
                $code = $tmpOne['code'];
                if (file_exists($tmpOne['thumb'])) {
                    $thumb = $tmpOne['thumb'];
                }
            }
            $sceneOne = (array) $this->dbConn($this->dbScene)
                ->where('id', $scene_id)
                ->first();
            $scene_name = empty($sceneOne) ? $sceneOne['title'] : '-';
            $reply_total = $this->dbConn($this->dbTmpReply)
                ->where('tmp_id', $v['tmp_id'])
                ->count();
            $arys[] = [
                'id' => $v['id'],
                'title' => $title,
                'scene_name' => $scene_name,
                'code' => $code,
                'prefixUrl' => env('DOMAIN').'/',
                'thumb' => $thumb,
                'detail' => mb_strlen($v['cnt'])>50 ? mb_substr($v['cnt'], 0, 50).'...' : $v['cnt'],
                'cnt' => $v['cnt'],
                'reply_total' => $reply_total,
                'addtime' => $v['addtime'],
            ];
        }
        return $this->toback(0, '', $arys, $total);
    }

    /**
     * 场景列表
     * @param array $pages【start，limit】
     * @return array
     */
    public function getSceneList($pages)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = $this->dbConn($this->dbScene)->where('del', 1);
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])
            ->orderBy('id', 'desc')
            ->get();
        if (!count($res)) {
            return $this->toback(-2, '没有记录');
        }
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            $arys[] = [
                'id' => $v['id'],
                'title' => $v['title'],
                'prefixUrl' => env('DOMAIN').'/',
                'thumb' => file_exists($v['thumb']) ? $v['thumb'] : '',
            ];
        }
        return $this->toback(0, '', $arys, $total);
    }

    /**
     * 场景内容列表
     * @param array $pages【start，limit】
     * @return array
     */
    public function getSceneCntList($pages, $scene_id)
    {
        $res = $this->dbConn($this->dbSceneCnt)
            ->where('del', 1)
            ->where('scene_id', $scene_id)
            ->orderBy('sort', 'desc')
            ->get();
        if (!count($res)) {
            return $this->toback(-2, '没有记录');
        }
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            if ($v['type'] > 1) {
                $cnt = file_exists($v['cnt']) ? env('DOMAIN').'/'.$v['cnt'] : '';
            } else {
                $cnt = $v['cnt'];
            }
            $arys[] = [
                'id' => $v['id'],
                'type' => $v['type'],
                'cnt' => $cnt,
            ];
        }
        return $this->toback(0, '', $arys);
    }
}